﻿using System;

namespace QuickShare.Vsix.SqlList.Commons
{
    class Lock : IDisposable
    {
        static bool isLocked = false;
        static string globalLockName = string.Empty;
        public bool IsSuccess { get; private set; }
        public string Message { get; private set; }

        //private Lock(string lockName)
        //{
        //    if(isLocked)
        //    {
        //        this.Message = globalLockName;
        //    }
        //    else
        //    {
        //        isLocked = true;
        //        globalLockName = lockName;
        //    }
        //}
        public void Dispose()
        {
            if(IsSuccess)
            {
                isLocked = false;
                globalLockName = string.Empty;
            }
        }

        public static Lock Get(string lockName)
        {
            if(isLocked)
            {
                return new Lock() { IsSuccess = false, Message = globalLockName };
            }
            else
            {
                isLocked = true;
                globalLockName = lockName;
                return new Lock() { IsSuccess = true };
            }
        }
    }
}
